package com.biz.primus.ms.payment.dao.po;

import com.biz.primus.base.enums.PaymentStatusEnum;
import com.biz.primus.model.payment.enums.PaymentTradeType;
import com.biz.primus.model.payment.enums.PaymentType;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import java.sql.Timestamp;
import javax.persistence.*;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

@Entity
@Table(name = "payment_order",
        indexes = {
                @Index(columnList = "mchId"),
                @Index(columnList = "mchId, mchOrderNo"),
                @Index(columnList = "paymentType")
        })
@Data
public class PaymentOrder extends BaseEntity {

    private static final long serialVersionUID = 7076411787442167785L;

    /**
     * 商户ID
     */
    private String mchId;

    /**
     * 商户订单号
     */
    private String mchOrderNo;

    /**
     * 商户退款单号
     */
    private String mchRefundNo;

    /**
     * 支付类型
     */
    private PaymentType paymentType;

    /**
     * 交易方式
     */
    private PaymentTradeType paymentTradeType;

    /**
     * 支付金额,单位分
     */
    private Integer amount;

    /**
     * 三位货币代码,人民币:cny
     */
    private String currency;

    /**
     * 支付状态
     */
    @Column
    @Convert(converter = PaymentStatusEnum.Converter.class)
    private PaymentStatusEnum paymentStatus = PaymentStatusEnum.CREATE_PAYMENT;

    /**
     * 客户端IP
     */
    private String clientIp;

    /**
     * 设备
     */
    private String device;

    /**
     * 商品标题
     */
    private String subject;

    /**
     * 商品描述信息
     */
    private String body;

    /**
     * 特定渠道发起时额外参数
     */
    private String extra;

    /**
     * 渠道商户ID
     */
    private String channelMchId;

    /**
     * 渠道订单号
     */
    private String tradeNo;

    /**
     * 渠道支付错误码
     */
    private String errCode;

    /**
     * 渠道支付错误描述
     */
    private String errMsg;

    /**
     * 扩展参数1
     */
    private String param1;

    /**
     * 扩展参数2
     */
    private String param2;

    /**
     * 通知地址
     */
    private String notifyUrl;

    /**
     * 通知次数
     */
    private Byte notifyCount;

    /**
     * 最后一次通知时间
     */
    private Timestamp lastNotifyTime;

    /**
     * 订单失效时间
     */
    private Timestamp expireTime;

    /**
     * 订单支付成功时间
     */
    private Timestamp paySuccessTime;

    /**
     * 退款成功时间
     */
    private Timestamp refundSuccessTime;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }

    public Boolean isExpired() {

        return expireTime == null || expireTime.getTime() < System.currentTimeMillis();
    }
}